package tech.aistar.corejava.day06;

import java.util.Arrays;

/**
 * Created by 李雨昂 on 2019/7/22.
 */
public class Emp {
    private int id;
    private String name;
    private int age;
    private Emp[] emps;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Emp[] getEmps() {
        return emps;
    }

    public void setEmps(Emp[] emps) {
        this.emps = emps;
    }

    public Emp(int id, String name, int age, Emp[] emps) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.emps = emps;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Emp{");
        sb.append("id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append(", age=").append(age);
        sb.append(", emps=").append(Arrays.toString(emps));
        sb.append('}');
        return sb.toString();
    }

    public void addEmp(Emp e) {
        if(null == emps ){
            System.out.println("本身就是下属!没有权限添加");
            return;
        }
        emps = Arrays.copyOf(emps,emps.length+1);

        emps[emps.length - 1] = e;
        System.out.println("有权限添加!");
    }

    //static   ?
    public void delById(int id){
        if(null == emps || emps.length == 0){
            System.out.println("删除失败!");
            return;
        }

        Emp[] temp = new Emp[emps.length - 1];

        int pos = 0;
        for (int i = 0; i < emps.length; i++) {
            if(emps[i].getId() != id){
                temp[pos++] = emps[i];
            }
        }
        emps = temp;
    }

    public void delByName(String name){
        if(null == emps || emps.length == 0){
            System.out.println("删除失败!");
            return;
        }

        int count = 0;
        for (int i = 0; i <emps.length; i++) {
            if(name.equals(emps[i].getName())){
                count++;
            }
        }
        if(count == 0){
            System.out.println("sorry,不存在!");
            return;
        }

        Emp[] temp = new Emp[emps.length - count];

        int pos = 0;
        for (int i = 0; i <emps.length; i++) {
            if( !name.equals(emps[i].getName())){
                temp[pos++] = emps[i];
            }
        }
        emps = temp;
    }

}
